package com.wangmengtao.upms.handle;

import com.wangmengtao.upms.dto.ServerResponse;
import com.wangmengtao.upms.exception.UpmsException;
import com.wangmengtao.upms.util.ServerResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: ExceptionHandle
 * @Description: DOTO
 * @Author: WangMengtao
 * @Date: 2019/7/14 21:49
 * @version: 1.0
 **/
@ControllerAdvice
public class ExceptionHandle {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ServerResponse handle(Exception e) {
        if (e instanceof UpmsException) {
            UpmsException upmsException = (UpmsException) e;
            return ServerResponseUtil.error(upmsException.getCode(),upmsException.getMessage());
        } else {
            logger.error("[系统异常] {}",e);
            return ServerResponseUtil.error(-1,"未知错误！");
        }

    }
}
